<template>
  <div class="add-form">
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="30%"  @close="() => formBase = {}">
      <el-form ref="dataForm" :model="formBase" :rules="rules" label-position="left" label-width="150px">
        <el-form-item label="选择员工：" prop="id">
          <el-input v-if="formBase.tmpId?true:false" v-model="formBase.name" disabled></el-input>
          <el-select v-if="formBase.tmpId?false:true" v-model="formBase.id" placeholder="请选择" clearable>
            <el-option
              v-for="item in employeeSimpleList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="formBase.username"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="formBase.password" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="passwordConfirm">
          <el-input v-model="formBase.passwordConfirm" autocomplete="off" show-password></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">取消</el-button>
        <el-button type="primary" @click="createData">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { updateData, addData, getSimpleList as getSimpleEmployeeList } from '@/api/employee'
export default {
  name: 'add',
  props: ['dialogTitle'],
  data() {
    var validatePass = (rule, value, callback) => {
      let ps = value
      if (ps === '') {
        callback(new Error('请输入密码'))
      } else if (ps.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        if (this.formBase.passwordConfirm !== '') {
          this.$refs.dataForm.validateField('passwordConfirm')
        }
        callback()
      }
    }
    var validatePassConfirm = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.formBase.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dialogFormVisible: false,
      formBase: {
        number: '',
        name: ''
      },
      hasUsername: false,
      employeeSimpleList: [],
      rules: {
          id: [
            { required: true, message: '请选择员工姓名', trigger: 'change' }
          ],
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, validator: validatePass, trigger: 'blur' }
          ],
          passwordConfirm: [
            { required: true, validator: validatePassConfirm, trigger: 'blur' }
          ]
        }
    }
  },
  computed: {
  },
  methods: {
    // 弹层显示
    dialogFormV(val) {
      this.dialogFormVisible = true
      if (val !== undefined) {
        this.formBase = val
      }
      if (!this.formBase.id) {
        this.doQuery(this.hasUsername)
      } else {
        this.formBase.tmpId = this.formBase.id
      }
    },
    // 弹层隐藏
    dialogFormH() {
      this.dialogFormVisible = false
    },

    // 表单提交
    createData() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          console.log(this.formBase)
          // if (this.formBase.name === '') {
          //   this.$message.error('员工为必选项')
          // }
          this.dialogFormH()
          let data = {
            ...this.formBase
          }
          if (this.formBase.id) {
            await updateData(data).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            await addData(this.formBase).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    },
    async doQuery(parent) {
      await getSimpleEmployeeList({hasUsername: false})
        .then(res => {
            const { data } = res
            this.employeeSimpleList = data
        })
        .catch(err => {
          console.log(err)
        })
    }
  },
  // 挂载结束

  mounted: function() {},
  // 创建完毕状态
  created() {
  },
  // 组件更新
  updated: function() {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
